﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RastinArgham.CRM.Business;
using System.IO;
using System.Drawing;

namespace RastinArgham.CRM.Web
{
    /// <summary>
    /// Summary description for GetImage
    /// </summary>
    public class GetImage : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            if ( String.IsNullOrEmpty( context.Request.QueryString["Type"] ) || String.IsNullOrEmpty( context.Request.QueryString["ContactId"] ) )
                return;

            CRMRepository crmRepository = new CRMRepository();
            int contactId = -1;

            if (!Int32.TryParse(context.Request.QueryString["ContactId"], out contactId))
                return;

            Contact c = crmRepository.GetContactById(contactId);
            if (c == null)
                return;

            byte[] image = null;
            if (context.Request.QueryString["Type"] == "CompanyContact_ID" )
            {
                CompanyContact cc = c.CompanyContacts.FirstOrDefault();

                if (cc.IdPhoto != null)
                {
                    image = cc.IdPhoto.ToArray();
                }
            }
            else if (context.Request.QueryString["Type"] == "CompanyContact_CertificatePhoto")
            {
                CompanyContact cc = c.CompanyContacts.FirstOrDefault();

                if (cc.ServicePhoto != null)
                {
                    image = cc.ServicePhoto.ToArray();
                }
            }
            else if (context.Request.QueryString["Type"] == "CompanyContact_ChargePhoto")
            {
                CompanyContact cc = c.CompanyContacts.FirstOrDefault();

                if (cc.ChargePhoto != null)
                {
                    image = cc.ChargePhoto.ToArray();
                }
            }
            else if (context.Request.QueryString["Type"] == "PersonalContact_ID")
            {
                PersonalContact pc = c.PersonalContacts.FirstOrDefault();

                if (pc.IdentityPhoto != null)
                {
                    image = pc.IdentityPhoto.ToArray();
                }
            }
            else if (context.Request.QueryString["Type"] == "PersonalContact_ServicePhoto")
            {
                PersonalContact pc = c.PersonalContacts.FirstOrDefault();

                if (pc.ServicePhoto != null)
                {
                    image = pc.ServicePhoto.ToArray();
                }
            }
            else if (context.Request.QueryString["Type"] == "OrderInstallationForm")
            {
                int orderId = -1;
                if (!String.IsNullOrEmpty(context.Request.QueryString["OrderId"]) && Int32.TryParse(context.Request.QueryString["OrderId"], out orderId))
                {
                    Order o = crmRepository.GetOrderById(orderId);
                    image = o.InstallationForm.ToArray();
                }
            }
            
            if (image == null || image.Length == 0)
                return;

            MemoryStream ms = new MemoryStream(image, 0, image.Length);
            Image dbImage = Image.FromStream(new MemoryStream(image));

            int width = dbImage.Width;
            int height = dbImage.Height;

            if (context.Request.QueryString["w"] != null && context.Request.QueryString["h"] != null)
            {
                width = Convert.ToInt32(context.Request.QueryString["w"]);
                height = Convert.ToInt32(context.Request.QueryString["h"]);
            }


            Image thumbnailImage = dbImage.GetThumbnailImage(width, height, null, new IntPtr());
            thumbnailImage.Save(ms, dbImage.RawFormat);
            Byte[] thumbnailByte = new Byte[ms.Length];
            ms.Position = 0;
            ms.Read(thumbnailByte, 0, Convert.ToInt32(ms.Length));
            context.Response.Clear();
            context.Response.BinaryWrite(thumbnailByte);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}